import 'dart:async';
import 'dart:io';

import 'package:dio/adapter.dart';
import 'package:flutter/material.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'Util.dart';

class ResultPage extends StatefulWidget {
  ResultPage({Key key, this.title, this.para}) : super(key: key);
  final String title;
  final Map<String, dynamic> para;
  @override
  _ResultPage createState() => _ResultPage();
}

class _ResultPage extends State<ResultPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadData();
  }

  void loadData() {
    SharedPreferences.getInstance().then((value) {
      setState(() {
        String domain =
            value.getString("buildings").toMap()["subdomain"].toString();
        print(domain);
        var dic = widget.para;
        dic["api_key"] = "xQITe6bZFUrmeJbkcKgkUzajUI6FGLA";
        dic["lang"] = "en";
        dic["block_no"] = "[]";
        dic["floor_no"] = "[]";
        dic["room_no"] = "[]";
        print(dic);
        var dio = Dio();
        if (ShareHandle.instance.proxy.length > 0) {
          (dio.httpClientAdapter as DefaultHttpClientAdapter)
              .onHttpClientCreate = (HttpClient client) {
            client.findProxy = (uri) {
              return ShareHandle.instance.proxy;
            };
          };
        }
        dio
            .post("${domain}/api/visitor/register", data: FormData.fromMap(dic))
            .then((response) {
          final result = response.toResult();
          if (result.item2) {
            setState(() {
              this.text = "身份已由以下職員確認！\n xxxxx\n xxxxx";
              this.img = "assets/images/icon_key_passed.png";
            });
          } else {
            showAlert(result.item4);
          }
        }).catchError((error) {
          print("error:${error}");
          showAlert(error);
        });
      });
    });
  }

  String text = "身份確認中，請耐心等待~謝謝!";
  String img = "assets/images/icon_key_checking.png";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title), actions: <Widget>[
        FlatButton(
          child: Text(
            "登出",
            style: TextStyle(color: Colors.white),
          ),
          onPressed: () {
            Navigator.of(context)
                .popUntil((route) => route.settings.name == "MyHome2Page");
          },
        )
      ]),
      body: Center(child: Column(
        children: <Widget>[
          Image(
            image: AssetImage(this.img),
          ),
          Text(
            text,
            textAlign: TextAlign.center,
          ),
          Row(
            children: <Widget>[
              MaterialButton(
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                height: 40,
                minWidth: 200,
                padding: EdgeInsets.all(0),
                color: Color(0xff4a701e),
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                onPressed: () {
                  Navigator.of(context).popUntil(
                          (route) => route.settings.name == "MyHome2Page");
                },
                child: Text("完成"),
              ),
              MaterialButton(
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                height: 40,
                minWidth: 200,
                padding: EdgeInsets.all(0),
                color: Color(0xff4a701e),
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                onPressed: () {
                  print("打印");
                },
                child: Text("列印通行證"),
              ),
            ],
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
          )
        ],
      ),),
    );
  }
}
